package androidx.gdata.spreadsheets.xml;

import org.w3c.dom.Node;

import java.util.List;

import androidx.gdata.Author;
import androidx.gdata.LinkUrl;
import androidx.gdata.spreadsheets.ListCollection;
import androidx.gdata.spreadsheets.ListEntry;
import androidx.gdata.xml.AuthorDeserializer;
import androidx.gdata.xml.ThingWithLinksDeserializer;

public class ListCollectionDeserializer
  extends ThingWithLinksDeserializer<ListCollection> {
  
  private static AuthorDeserializer authorBuilder = new AuthorDeserializer();
  private static ListEntryDeserializer listEntryBuilder =
    new ListEntryDeserializer();
  
  @Override
  public ListCollection build(Node feedNode) {
    node = feedNode;
    String id = getText("id");
    String updated = getText("updated");
    Node authorNode = getChildNode("author");
    Author author = authorBuilder.build(authorNode);
    String title = getText("title");
    LinkUrl[] links = getLinks();
    List<Node> entryNodes = getChildNodes("entry");
    ListEntry[] entries = new ListEntry[entryNodes.size()];
    int i = 0;
    for (Node entryNode : entryNodes) {
      ListEntry entry = listEntryBuilder.build(entryNode);
      entries[i++] = entry;
    }
    return new ListCollection(id, updated, title, links, author, entries);
  }

}
